<?php

/**
	$Id: cartesian_product.php 10 2011-03-07 09:16:18Z eugim.migue@gmail.com $
*/

function print_c($s){

	
	global $enclose;
	$cnt = count($s);
	$newLine = isset($_SERVER['REQUEST_METHOD']) ? "<br>" : "\n";
	echo "Total elements: " . $cnt . $newLine;
	
	$cnt--;
	echo "array($newLine";
	foreach($s as $i=>$v){
		/*
		if( $enclose ){
			$v = explode(",", $v);
			foreach($v as $key=>&$x){
				$x = '"' . $x . '"';
			}
			$v = implode(", ", $v);
		}
		*/
		//echo /*$i+1 . ".)*/ "array($v) " . ($i == $cnt ? "" : ',') . "$newLine";
		echo "array(";
		$s = '';
		foreach($v as $k=>$l){
			$s .= ", " . "'$k'=>" . (is_string($l) ? "'$l'" : (empty($l) ? (is_bool($l) ? 'FALSE' : $l) : $l));
		}
		$s = ltrim($s, ", ");
		echo $s;
		echo ") " . ($i == $cnt ? "" : ',') . "$newLine";
	}
	echo ");";
}

function combination($set, $r = NULL){
	
	return $s;
}

function power_set($set, $includeEmptySet = TRUE){
	
	return $s;
}

function cartesian_product( array $domains = array() ){
	

	static $r = array();
	static $s = array();
	foreach($domains as $dName=>$domain){
		foreach($domain as $value){
			$r[$dName] = $value;
			if( count($domains) > 1){
				cartesian_product(array_slice($domains, 1));
			}
			else{
				$s[] = $r;
			}
		}
		break;
	}
	return $s;
	
	
	

	
	/*
	$d1 = (array)$domains[0];
	$d2 = (array)$domains[1];
	global $enclose;
	$s = array();
	foreach($d1 as $v1){
		foreach($d2 as $v2){
			
			$s[] =  $v1 . "," . $v2;
			$s[] = array
		}
	}
	if( $dCnt - 2 > 0 ){
		$nD = array($s);
		for($i = 1; $i < $dCnt-1; $i++){
			$nD[$i] = $domains[$i+1];
		}
		$s = cartesian_product($nD);	
	}
	return $s;
	*/
}




$enclose = TRUE;
$domains = array(
	/*
	array("Selector Specified", "Selector not Specified"),
	array("Has Default Selector", "No Default Selector"),
	//array("Selector Exists", "Selector Does not Exist"),
	array("Selector exists - components matched", "Selector exists - components does not matched", "selector does not exist"),
	array("Poss. rep. spec. default", "Poss rep. user specified")
	*/
	/*
	array("'args'=>array('a'=>1, 'b'=>2, 'c'=>3)"),
	array(
		"'op'=>'op1', 'expected'=>array('a'=>1, 'b'=>2, 'c'=>3), 'exception'=>FALSE",
		"'op'=>'op2_1', 'expected'=>array('a'=>1, 'b'=>2, 'c'=>3, 'd'=>4), 'exception'=>FALSE",
		"'op'=>'op2_2', 'exception'=>TRUE",
		"'op'=>'op3_1', 'expected'=>array('a'=>1, 'b'=>2), 'exception'=>FALSE",
		"'op'=>'op3_2', 'expected'=>array(), 'exception'=>FALSE",
		"'op'=>'op4_1', 'expected'=>array('f'=>1, 'g'=>2), 'exception'=>FALSE",
		"'op'=>'op4_2', 'exception'=>TRUE"
	)
	*/
	"hasSelectorSpec"=>array(TRUE, FALSE),//array('SELECTOR SPEC.' , 'NO SELECTOR SPEC.'),
	"hasDefaultSel"=>array(TRUE, FALSE),
	'hasArgSpec'=>array(TRUE, FALSE),
	'hasDefaultArg'=>array(TRUE, FALSE),
	'case'=>array("SELECTOR_NOT_IN_POSS_REP")
	//"defaultSel"=>array('DEF. SELECTOR' , 'NO DEF. SELECTOR'),
	//"argSpec"=>array('ARGS SPEC.' , 'NO ARGS SPEC.'),
	//"defaultArg"=>array('DEF. ARGS' , 'NO DEF. ARGS'),
	//"inPr"=>array('SELECTOR NOT IN PR')
);
//cartesian_product($domains);
//print_r(cartesian_product($domains));
print_c(cartesian_product($domains));